clc;clear;clearvars;
load('iris_X.mat', 'iris');
x = iris;
load('iris_Y.mat', 'iris');
y = iris;
n = size(x, 1);
% num表示有多少个类标签
num = 3;

for k = 1 : 10
    % for k1 = 1 : 5
    for k1 = 5
        u = memberships(x, y, num, k1);
        test_y = zeros(n ,1);
        for i = 1 : n
            % 在样本集中选择一个样本作为测试集，其他样本作为训练集
            test_x = x(i, :);
            if i == 1
                ind = i + 1 : n;
                test_y(i, :) = fknn(x(ind, :), test_x, k, num, u);
            else
                ind = [1 : i - 1, i + 1 : n];
                test_y(i, :) = fknn(x(ind, :), test_x, k, num, u);
            end
        end
        err_num = sum(test_y ~= y);
        err_rate = err_num / n;
        fprintf("iris total: %d k: %d k1: %d err_num: %d err_rate: %.4f\n",n,k,k1,err_num,err_rate);
    end
end